#服务端
from sanic import Sanic
from sanic import response
import json
import _query

app = Sanic(__name__)

@app.listener("before_server_start")
async def do_something(app,loop):
    return 0

@app.route("/")
async def test(req):
    #获取查询条件
    query_json_str = req.args.get('query','null')
    #非法查询
    if query_json_str == 'null':
        return response.text('no query,please relay query data...')
    #转换为dict类型
    query_json = json.loads(query_json_str)
    #查询信息
    print(f'凸多边形边数:{len(query_json["coordinates"])}')
    print(f'数据点情况:')
    point_list = query_json["coordinates"]
    for i in range(len(point_list)):
        print(f'{point_list[i]}')
    #获取包裹的矩阵
    Rec = _query.findRectangle(point_list)
    qdata = _query.findPointByRectangle(Rec)
    print(f'qdata type:{type(qdata)} len:{len(qdata)}')
    #转为json,返回客户端
    jsonqdata = json.jsonArr = json.dumps(qdata, ensure_ascii=False)
    return response.json(jsonqdata)


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)